]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
st: Stop implementing custom refcounting
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 25 Jan 2023 20:45:49 +0000 (21:45 +0100)
committerMarge Bot <marge-bot@gnome.org>
Sun, 19 Mar 2023 11:51:01 +0000 (11:51 +0000)
Glib has generic RcBox/ArcBox types nowadays that can be used to
add ref-counting to a plain struct types, use those instead.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2679>

src/st/st-icon-cache.c
src/st/st-icon-colors.c
src/st/st-icon-colors.h
src/st/st-shadow.c
src/st/st-shadow.h

index 55638a5cf9a03296237561c9bf817c30d2659882..99c3692f5e41678ce3ece645503bc3ae00dc58df 100644 (file)
 
 
 struct _StIconCache {
-  int ref_count;
-
   GMappedFile *map;
   char *buffer;
 
   guint32 last_chain_offset;
 };
 
+static void
+clear_icon_cache (gpointer data)
+{
+  StIconCache *cache = data;
+  g_clear_pointer (&cache->map, g_mapped_file_unref);
+}
+
 StIconCache *
 st_icon_cache_ref (StIconCache *cache)
 {
-  cache->ref_count++;
-  return cache;
+  return g_atomic_rc_box_acquire (cache);
 }
 
 void
 st_icon_cache_unref (StIconCache *cache)
 {
-  cache->ref_count --;
-
-  if (cache->ref_count == 0)
-    {
-      g_debug ("unmapping icon cache");
-
-      if (cache->map)
-        g_mapped_file_unref (cache->map);
-      g_free (cache);
-    }
+  g_atomic_rc_box_release_full (cache, clear_icon_cache);
 }
 
 StIconCache *
@@ -115,8 +110,7 @@ st_icon_cache_new_for_path (const char *path)
 
   g_debug ("found icon cache for %s", path);
 
-  cache = g_new0 (StIconCache, 1);
-  cache->ref_count = 1;
+  cache = g_atomic_rc_box_new0 (StIconCache);
   cache->map = map;
   cache->buffer = g_mapped_file_get_contents (map);
 
@@ -133,9 +127,7 @@ st_icon_cache_new (const char *data)
 {
   StIconCache *cache;
 
-  cache = g_new0 (StIconCache, 1);
-  cache->ref_count = 1;
-  cache->map = NULL;
+  cache = g_atomic_rc_box_new0 (StIconCache);
   cache->buffer = (char *)data;
 
   return cache;
index c6a082add37fb24cc4c42d83910342893ea66599..89257e0b7e8007f74e7dfadd7573d1fce1f8264e 100644 (file)
 StIconColors *
 st_icon_colors_new (void)
 {
-  StIconColors *colors;
-
-  colors = g_new0 (StIconColors, 1);
-  colors->ref_count = 1;
-
-  return colors;
+  return g_atomic_rc_box_new0 (StIconColors);
 }
 
 /**
@@ -51,10 +46,8 @@ StIconColors *
 st_icon_colors_ref (StIconColors *colors)
 {
   g_return_val_if_fail (colors != NULL, NULL);
-  g_return_val_if_fail (colors->ref_count > 0, colors);
 
-  g_atomic_int_inc ((volatile int *)&colors->ref_count);
-  return colors;
+  return g_atomic_rc_box_acquire (colors);
 }
 
 /**
@@ -69,10 +62,8 @@ void
 st_icon_colors_unref (StIconColors *colors)
 {
   g_return_if_fail (colors != NULL);
-  g_return_if_fail (colors->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test ((volatile int *)&colors->ref_count))
-    g_free (colors);
+  g_atomic_rc_box_release (colors);
 }
 
 /**
index e994a7522ac4fa9ffd3328ed0a935978fec5fccc..764825dddf5d4084c74184344165e8f1843e5e2d 100644 (file)
@@ -21,8 +21,6 @@ typedef struct _StIconColors StIconColors;
  * icon.
  */
 struct _StIconColors {
-  volatile guint ref_count;
-
   ClutterColor foreground;
   ClutterColor warning;
   ClutterColor error;
index 0a8e319ad45b11cebb34dbd3e2950445fbf1fe8f..97edbe90c3ba4627123f565315bcbef1c891a1cb 100644 (file)
@@ -59,7 +59,7 @@ st_shadow_new (ClutterColor *color,
 {
   StShadow *shadow;
 
-  shadow = g_new (StShadow, 1);
+  shadow = g_atomic_rc_box_new (StShadow);
 
   shadow->color     = *color;
   shadow->xoffset   = xoffset;
@@ -67,7 +67,6 @@ st_shadow_new (ClutterColor *color,
   shadow->blur      = blur;
   shadow->spread    = spread;
   shadow->inset     = inset;
-  shadow->ref_count = 1;
 
   return shadow;
 }
@@ -84,10 +83,8 @@ StShadow *
 st_shadow_ref (StShadow *shadow)
 {
   g_return_val_if_fail (shadow != NULL, NULL);
-  g_return_val_if_fail (shadow->ref_count > 0, shadow);
 
-  g_atomic_int_inc (&shadow->ref_count);
-  return shadow;
+  return g_atomic_rc_box_acquire (shadow);
 }
 
 /**
@@ -102,10 +99,8 @@ void
 st_shadow_unref (StShadow *shadow)
 {
   g_return_if_fail (shadow != NULL);
-  g_return_if_fail (shadow->ref_count > 0);
 
-  if (g_atomic_int_dec_and_test (&shadow->ref_count))
-    g_free (shadow);
+  g_atomic_rc_box_release (shadow);
 }
 
 /**
index 267d48f7b6d5c32f37e4b9a7d3c9c97898e39006..e4271c59cf10a61886e30f7c060ee6f86881ae96 100644 (file)
@@ -51,7 +51,6 @@ struct _StShadow {
     gdouble      blur;
     gdouble      spread;
     gboolean     inset;
-    volatile int ref_count;
 };
 
 GType     st_shadow_get_type (void) G_GNUC_CONST;